package cn.zooz.admin.config.web;

import cn.zooz.common.util.filter.xss.XssFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;

import javax.servlet.Filter;
import javax.servlet.Servlet;

/**
 * Created by Bradish7Y on 2017/9/1.
 */
@Configuration
public class WebConfig {
    /**
     * 注册自己的HttpMessageConverter
     */
//    @Bean
//    public HttpMessageConverters fastJsonHttpMessageConverters() {
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        HttpMessageConverter<?> converter = fastConverter;
//        return new HttpMessageConverters(converter);
//    }

    /**
     * 注册监听器，注册多个，声明多个
     */
//    public ServletListenerRegistrationBean<> servletListenerRegistrationBean(){
//        ServletListenerRegistrationBean<> listner = new ServletListenerRegistrationBean();
//
//        return listner;
//    }


    /**
     * 注册xss filter
     */
    @Bean
    public FilterRegistrationBean xssFilterBean() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(xssFilter());
        filter.addUrlPatterns("/*");
        filter.setName("xssFilter");
        //filter.setOrder(1);
        return filter;
    }


    /**
     * 注册druid servlet
     */
    @Bean
    public ServletRegistrationBean statViewServletBean() {
        ServletRegistrationBean servlet = new ServletRegistrationBean();

        servlet.setServlet(statViewServlet());
        servlet.addUrlMappings("/druid/*");
        servlet.addInitParameter("loginUsername", "druid");
        servlet.addInitParameter("loginPassword", "123456");
        servlet.addInitParameter("resetEnable", "true");
        servlet.addInitParameter("allow", "127.0.0.1");

        return servlet;
    }

    @Bean
    public FilterRegistrationBean webStatFilterBean() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(webStatFilter());
        filter.addUrlPatterns("/*");
        filter.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        filter.addInitParameter("profileEnable", "true");
//        filter.addInitParameter("principalCookieName", "USER_COOKIE");
//        filter.addInitParameter("principalSessionName", "USER_SESSION");
        filter.setName("druidWebStatFilter");
        //filter.setOrder(1);
        return filter;
    }

    @Bean

    public FilterRegistrationBean resourceUrlEncodingFilterBean() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(resourceUrlEncodingFilter());
        filter.addUrlPatterns("/*");

        //filter.setOrder(1);
        return filter;
    }

    @Bean
    public Filter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }

    /**
     * druid filter
     */
    @Bean
    public Filter webStatFilter() {
        return new WebStatFilter();
    }

    /**
     * xss过滤
     */
    @Bean
    public Filter xssFilter() {
        return new XssFilter();
    }

    /**
     * druid servlet
     */
    @Bean
    public Servlet statViewServlet() {
        return new StatViewServlet();
    }

}
